//
//  MainShopCell.m
//  Ufan
//
//  Created by yangqingluo on 15/11/12.
//  Copyright © 2015年 yangqingluo. All rights reserved.
//

#import "MainProductSellCell.h"
#import "UIImageView+EMWebCache.h"
#import "CWStarRateView.h"

@interface MainProductSellCell(){
    
}

@property (nonatomic, strong) UIImageView *mainImageView;
@property (nonatomic, strong) UIButton *detailButton;
@property (strong, nonatomic) UIView *bottomLineView;
@property (strong, nonatomic) CWStarRateView *starRateView;
@property (strong, nonatomic) UILabel *priceLabel;

@end

@implementation MainProductSellCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.textLabel.font = [UIFont systemFontOfSize:16.0];
        self.textLabel.textColor = [UIColor blackColor];
        self.detailTextLabel.font = [UIFont systemFontOfSize:16.0];
        self.detailTextLabel.textColor = [UIColor blackColor];
        
        _bottomLineView = [[UIView alloc] init];
        _bottomLineView.backgroundColor = UFCOLOR_GRAYLINE;
        [self.contentView addSubview:_bottomLineView];
        
        self.starRateView = [[CWStarRateView alloc] initWithFrame:CGRectMake(0, 0, 100, 20) numberOfStars:5];
//        self.starRateView.scorePercent = 0.3;
//        self.starRateView.allowIncompleteStar = YES;
        self.starRateView.hasAnimation = YES;
        self.starRateView.userInteractionEnabled = NO;
        [self.contentView addSubview:self.starRateView];
        
        _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 16)];
        _priceLabel.font = [UIFont systemFontOfSize:12.0];
        _priceLabel.textColor = self.detailTextLabel.textColor;
        _priceLabel.textAlignment = NSTextAlignmentRight;
        _priceLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_priceLabel];
    }
    return self;
}



-(void)layoutSubviews{
    [super layoutSubviews];
    
    NSURL *imageUrl = [NSURL URLWithString:self.product.mainUrl];
    [self.mainImageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@""]];
    
    

    self.textLabel.text = self.product.name;
    self.textLabel.frame = CGRectMake(self.mainImageView.right + 10, 10, self.width - (self.mainImageView.right + 10), 20);
    
    self.detailTextLabel.text = [NSString stringWithFormat:@"预定数量：%lu份",(unsigned long)self.product.orderCount];
    self.detailTextLabel.frame = CGRectMake(self.textLabel.left, self.height - 30, self.width - self.textLabel.left, 20);
    
    [self.starRateView setScorePercent:(self.product.starCount / 5.0)];
    self.starRateView.center = CGPointMake(self.textLabel.left + 0.5 * self.starRateView.width, self.detailTextLabel.top -  self.starRateView.height);
    
    self.priceLabel.center = CGPointMake(self.width - 10 - 0.5 * self.priceLabel.width, self.starRateView.center.y);
    self.priceLabel.text = [NSString stringWithFormat:@"单价：%@元",self.product.price];
    
    _bottomLineView.frame = CGRectMake(self.mainImageView.left, self.height - 1, self.width - self.mainImageView.left, 1);
}


+(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}

- (void)buttonClick:(UIButton *)button{
    
    
}


#pragma getter

- (UIImageView *)mainImageView{
    if (!_mainImageView) {
        _mainImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, self.height - 10, self.height - 10)];
        [self.contentView addSubview:_mainImageView];
    }
    
    return _mainImageView;
}



- (UIButton *)detailButton{
    if (!_detailButton) {
        _detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _detailButton.frame = CGRectMake(self.width - 110, self.height - 35, 100, 30);
        _detailButton.layer.cornerRadius = 5;
        _detailButton.layer.masksToBounds = YES;
        [_detailButton setBackgroundColor:[UIColor orangeColor]];
        [_detailButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _detailButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        [_detailButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_detailButton];
    }
    
    return _detailButton;
}

@end
